package com.yiduan.apollocrm.views.mediators
{
	import com.pialabs.eskimo.events.ContextableListEvent;
	import com.yiduan.apollocrm.models.Campaign;
	import com.yiduan.apollocrm.views.CampaignEditView;
	import com.yiduan.apollocrm.views.CampaignView;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.TextOperationEvent;
	
	public class CampaignMediator extends Mediator
	{
		[Inject]
		public var campaignView:CampaignView;
		
		private var filterText:String="";
		
		public function CampaignMediator()
		{
			super();
		}
		
		override public function onRegister():void
		{
			initLeadDataProvider();
			campaignView.editCampaignBtn.addEventListener(MouseEvent.CLICK,goToCompaignEditView);
			campaignView.filterCampaignTextInput.addEventListener(TextOperationEvent.CHANGE,changeFilter);
			campaignView.campaignList.addEventListener(ContextableListEvent.CONTEX_MENU_ITEM_CLICK,campaignListContextMenuHandler);
		}
		
		protected function goToCompaignEditView(event:MouseEvent):void
		{
			campaignView.navigator.pushView(CampaignEditView);
		}
		
		protected function initLeadDataProvider():void
		{	
			campaignView.campaignListDataProvider = fakeCampaigns();
			campaignView.campaignListDataProvider.filterFunction = filterCampaigns;			
		}
		
		protected function changeFilter(event:TextOperationEvent):void
		{
			filterText = campaignView.filterCampaignTextInput.text;
			campaignView.campaignListDataProvider.refresh();
		}
		
		private function filterCampaigns(o:Object):Boolean
		{
			var campaign:Campaign = o as Campaign;
			if (campaign!=null)
			{
				if (campaign.campaignName.toUpperCase().indexOf(filterText) != -1)
				{
					return true;
				}
				return false;
			}
			return false;
		}
		
		private function fakeCampaigns():ArrayCollection
		{
			var arrayCollection:ArrayCollection = new ArrayCollection();
			var campaign1:Campaign = new Campaign();
			campaign1.id =1;
			campaign1.ownerName="张三";
			campaign1.campaignName="市场活动1";
			campaign1.status="无";
			campaign1.enable=true;
			campaign1.startDate="14082011";
			campaign1.endDate="14092011";
			campaign1.expectedCost=100.1;
			campaign1.type="广告";		
			
			var campaign2:Campaign = new Campaign();
			campaign2.id = 2;
			campaign2.ownerName="李四";
			campaign2.campaignName="市场活动2";
			campaign2.status="计划中";
			campaign2.enable=false;
			campaign2.startDate="14082011";
			campaign2.endDate="14092011";
			campaign2.expectedCost=10.9;
			campaign2.type="无";			
			
			var campaign3:Campaign = new Campaign();
			campaign3.id = 3;
			campaign3.ownerName="王五";
			campaign3.campaignName="市场活动3";
			campaign3.status="完成";
			campaign3.enable=true;
			campaign3.startDate="14082011";
			campaign3.endDate="14092011";
			campaign3.expectedCost=200;
			campaign3.type="电子邮件";
			
			arrayCollection.addItem(campaign1);
			arrayCollection.addItem(campaign2);
			arrayCollection.addItem(campaign3);			
			
			return arrayCollection;
		}
		
		protected function campaignListContextMenuHandler(event:ContextableListEvent):void
		{
			var campaign:Campaign = event.item as Campaign;	
			
			if (event.menuItem.name == "editContextMenu")
			{
				goToCampaignEditView(campaign);
			}
			
			else if (event.menuItem.name == "deleteContextMenu")
			{
				deleteCampaign(event.itemIndex);
			}
		}
		
		private function goToCampaignEditView(campaign:Campaign):void
		{			
			campaignView.navigator.pushView(CampaignEditView,campaign);
		}
		
		private function deleteCampaign(index:int):void
		{
			campaignView.campaignList.dataProvider.removeItemAt(index);
		}
	}
}